package com.lanyam.travel.base.controller;


import com.lanyam.travel.base.utils.JSONResult;
import com.lanyam.travel.base.entity.Dictionary;
import com.lanyam.travel.base.entity.vo.DictionaryVo;
import com.lanyam.travel.base.service.IDictionaryService;
import com.lanyam.travel.base.utils.JsonMapper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.core.type.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;


@RestController
@RequestMapping("/dictionary")
public class DictionaryController {

    @Autowired
    private IDictionaryService dictionaryService;

    /**
     * 分页查询数据
     */
    @GetMapping("/getPage")
    public IPage<DictionaryVo> getPage (String queryMap,
                                                @RequestParam(name="page", defaultValue = "1") Integer page,
                                                @RequestParam(name="rows", defaultValue = "10") Integer rows) {
        Map<String, Object> query = JsonMapper.string2Obj(queryMap,new TypeReference<Map<String, Object>>() {});
        query = query==null?new HashMap<>():query;
        IPage<DictionaryVo> pageInfo = dictionaryService.getPageDictionary(query, page, rows);
        return pageInfo;
    }

    /**
     * 新增数据
     */
    @PostMapping("/add")
    public JSONResult add (@RequestBody Dictionary dictionary) {
        JSONResult result = null;
        try{
            dictionaryService.addDictionary(dictionary);
            result = JSONResult.ok();
        }  catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result = JSONResult.errorMsg(msg);
        }
        return result;
    }

    /**
     * 编辑数据
     */
    @PostMapping("/update")
    public JSONResult update (@RequestBody Dictionary dictionary) {
        JSONResult result = null;
        try{
            dictionaryService.updateDictionary(dictionary);
            result = JSONResult.ok();
        }  catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result = JSONResult.errorMsg(msg);
        }
        return result;
    }

    /**
     * 删除数据
     */
    @PostMapping("/delById")
    public JSONResult delById (String id) {
        JSONResult result = null;
        try {
            dictionaryService.delById(id);
            result = JSONResult.ok();
        }  catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result = JSONResult.errorMsg(msg);
        }
        return result;
    }

    /**
     * 根据字典类型查询字典列表
     */
    @PostMapping("/getListByType")
    public List<DictionaryVo> getListByType(String dictType) {
        List<DictionaryVo> listByType = null;
        try {
            listByType = dictionaryService.getListByType(dictType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listByType;
    }
    @PostMapping("/getListBySuperId")
    public List<Dictionary> getListBySuperId(String superId,String dictType) {
        List<Dictionary> listBySuperId= null;
        try {
            listBySuperId = dictionaryService.getListBySuperId(superId,dictType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listBySuperId;
    }
}